<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Employee;
use App\Models\PersonnelDynamics;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class EmployeeController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];


        if ( $request->employee_id) {
            $employee = Employee::findOrFail($request->employee_id);
            $employee->mtime = time();
        } else {
            $employee = new Employee();
            $employee->ctime = time();

        }
        $employee->name = $request->name;
        $employee->gender = $request->gender;
        $employee->mobile = $request->mobile;
        $employee->email = $request->email;
        $employee->id_type = $request->id_type;
        $employee->id_no = $request->id_no;
        $employee->birthday = $request->birthday;
        $employee->is_marry = $request->is_marry;
        $employee->is_fertile = $request->is_fertile;
        $employee->country = $request->country;
        $employee->nation = $request->nation;
        $employee->politic_countenance = $request->politic_countenance;
        $employee->wechat = $request->wechat;
        $employee->native_place = $request->native_place;
        $employee->native_city = $request->native_city;
        $employee->native_address = $request->native_address;
        $employee->highest_education = $request->highest_education;
        $employee->school = $request->school;
        $employee->major = $request->major;
        $employee->graduation_time = $request->graduation_time;
        $employee->learning_modality = $request->learning_modality;
        $employee->qq = $request->qq;
        $employee->telphone = $request->telphone;
        $employee->extension_number = $request->extension_number;
        $employee->residential_address = $request->residential_address;
        $employee->emergency_person = $request->emergency_person;
        $employee->emergency_mobile = $request->emergency_mobile;
        $employee->bank_name = $request->bank_name;
        $employee->bankcard_no = $request->bankcard_no;
        $employee->bankcard_city = $request->bankcard_city;
        $employee->social_account = $request->social_account;
        $employee->accumulation_fund = $request->accumulation_fund;
        $employee->entry_time = $request->entry_time;
        $employee->probation_period = $request->probation_period;
        $employee->company = $request->company;
        $employee->company_id = $company_id;
        $employee->organization_name = $request->organization_name;
        $employee->organization_id = $request->organization_id;
        $employee->post = $request->post;
        $employee->post_id = $request->post_id;
        $employee->position = $request->position;
        $employee->post_level = $request->post_level;
        $employee->work_nature = $request->work_nature;
        $employee->employee_status = $request->employee_status;
        $employee->work_no = $request->work_no;
        $employee->work_place = $request->work_place;
        $employee->work_city = $request->work_place;
        $employee->attendance_no = $request->attendance_no;
        $employee->uid = $uid;

        $employee->save();

        //添加人事动态
        $content = $userInfo['name']."添加了员工".$request->name;
        PersonnelDynamics::create([
            'company_id'=>$company_id,
            'organization_id'=>$request->organization_id,
            'content'=>$content,
            'status'=>1,
            'uid'=>$uid,
            'ctime'=>time(),
            'mtime'=>time(),
        ]);

        return showError(Error::ERR_OK,['employee_id'=>$employee->employee_id]);

    }

    public function show(Request $request) {
        $employee = Employee::findOrFail($request->employee_id);
        return showError(Error::ERR_OK,['employee'=>$employee]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }
        $mobile = $request->input('mobile');
        if (!empty($mobile))
        {
            $where['mobile'] = $mobile;
        }
        $employee_status = $request->input('employee_status');
        if (!empty($employee_status))
        {
            $where['employee_status'] = $employee_status;
        }
        $work_nature = $request->input('work_nature');
        if (!empty($work_nature))
        {
            $where['work_nature'] = $work_nature;
        }

        $where['company_id'] = $company_id;
        $where['status'] = 1;


        $list = Employee::getList($where);

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function getCount(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
//        $company_id = 4;
        $count1 = DB::select("SELECT employee_status,count(*) as employee_cnt FROM zlhr_employee WHERE company_id = $company_id and status = 1 GROUP BY employee_status");
        $count2 = DB::select("SELECT work_nature,count(*) as work_nature_cnt FROM zlhr_employee WHERE company_id = $company_id and status = 1 GROUP BY work_nature");

        return showError(Error::ERR_OK,['list1'=>$count1,'list2'=>$count2]);
    }


}
